package cn.test.house.controllers;

import cn.test.house.beans.ReturnData;
import cn.test.house.beans.User;
import cn.test.house.services.UserService;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonAlias;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /*
   查询所有用户信息
     */
    @GetMapping("/getAllUsers")
    @ResponseBody
    public String getAllUsers(HttpServletRequest request) {
        //获取当前页
        String nowPage = request.getParameter("page");
        //获取页面大小
        String pageSize = request.getParameter("limit");
        ReturnData returnData = new ReturnData();
        returnData.setCode(0);
        returnData.setCount(userService.getUsers().size());
        List<User> usersByPage = userService.getUsersByPage(Integer.parseInt(nowPage), Integer.parseInt(pageSize));
        returnData.setData(usersByPage);
        return JSON.toJSONString(returnData);
    }

    /*
    添加用户
     */
    @PostMapping("/addUser")
    @ResponseBody
    public String addUser(User user) {
        System.out.println(user);
        Map map = new HashMap();
        Integer integer = userService.addUser(user);
        if (integer > 0) {
            map.put("type", 1);
        } else {
            map.put("type", 0);
        }
        return JSON.toJSONString(map);
    }

    /*
    批量删除
     */
    @PostMapping("delAllUser")
    @ResponseBody
    public String delAllUser(HttpServletRequest request) {
        Integer integer = 0;
        String idArry = request.getParameter("idArry");
        //JSON对象转为java对象
        List<User> users = JSON.parseArray(idArry, User.class);
        //定义一个存放id的集合
        List list=new ArrayList();
        for (User u : users) {
            //把获取的到id存到集合
            list.add(u.getId());
        }
        //把要删除的id整合 进行批量删除
        integer= userService.delAllUser(list);
        Map map = new HashMap();
        if (integer > 0) {
            map.put("type", 1);
        } else {
            map.put("type", 0);
        }
        return JSON.toJSONString(map);
    }


    //删除单个
    @RequestMapping("deleteUserById")
    @ResponseBody
    public String deleteUserById(HttpServletRequest request) {
        Integer integer=0;
        String id = request.getParameter("id");
        if(id!=null){
            integer = userService.deleteUserById(Integer.parseInt(id));
        }
        ReturnData returnData = new ReturnData();
        returnData.setCode(integer);
        return JSON.toJSONString(returnData);
    }


   /*
   查询单个用户
    */
   @RequestMapping("/getUserById")
    public  String getUserById(HttpServletRequest request){
       String id = request.getParameter("id");
       System.out.println("ID:"+id);
       User userById = userService.getUserById(Integer.parseInt(id));
       request.setAttribute("user",userById);
       return "view_user";
   }


   /*
   根据姓名和性别查询
    */
   @RequestMapping("/getUsersByNameAndSex")
   @ResponseBody
    public  String getUsersByNameAndSex(HttpServletRequest request){
       String name = request.getParameter("name");
       String sex = request.getParameter("sex");

//       System.out.println("*****"+null+" "+sex);

       User user=new User();
       user.setName(name);
       user.setSex(sex);
       List<User> userByNameAndSex = userService.getUserByNameAndSex(user);

//       request.setAttribute("users",userByNameAndSex);
       ReturnData returnData=new ReturnData();
       returnData.setData(userByNameAndSex);
       returnData.setCode(0);
       returnData.setMsg("");



       return JSON.toJSONString(returnData);
   }


   /*
   用户编辑
    */
   @RequestMapping("/updateUser")
    public String updateUser(HttpServletRequest request){
       String id = request.getParameter("id");
       userService.getUserById(Integer.parseInt(id));
       return "";
   }



}
